package com.kingint.web.admin.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.validateUtil.ValidList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.SchedulingService;
import com.kingint.mbg.bas.entity.Scheduling;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @描述：排班控制类
 * @作者: wxh
 * @日期: 2021-01-23
 */
@RestController
@RequestMapping("/scheduling")
@Api(value = "SchedulingController", description = "排班")
@CrossOrigin
public class SchedulingController{

    @Autowired
    public SchedulingService schedulingService;

    @Autowired
    private UmsAdminService umsAdminService;


    @ApiOperation(value = "添加排班")
    @PostMapping("/create")
    //@PreAuthorize("hasAuthority('scheduling:create')")
    public Object create(@Validated(Insert.class) @RequestBody Scheduling scheduling) throws ParseException {
        UmsMember currentMember = umsAdminService.getCurrentMember();
        if(StringUtils.isEmpty(currentMember)){
           return K.error();
        }




        QueryWrapper<Scheduling> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("hotel_id",currentMember.getHotelId());
        List<Scheduling> list = schedulingService.list(queryWrapper);
        if(!StringUtils.isEmpty(list)){
            for (Scheduling scheduling1 : list) {
                if(scheduling1.getPeriod().intValue()==scheduling.getPeriod().intValue()){
                    return K.error().message("此排班已经添加，如需要请去修改");
                }
            }
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        Date frontStarTime = dateFormat.parse(scheduling.getFrontStarTime());
        frontStarTime.setYear(2021);
        Date frontEndTime =dateFormat.parse( scheduling.getFrontEndTime());
        frontEndTime.setYear(2021);

        scheduling.setSchedulingStart(frontStarTime);
        scheduling.setSchedulingEnd(frontEndTime);
        scheduling.setHotelId(currentMember.getHotelId());

        Boolean bool=schedulingService.save(scheduling);
        if(bool){
            return K.ok().data(scheduling.getId());
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "批量添加排班")
    @PostMapping("/insertList")
   // @PreAuthorize("hasAuthority('scheduling:insertList')")
    public Object create(@RequestBody @Validated(Insert.class)  ValidList<Scheduling> scheduling){
        Boolean bool=schedulingService.saveOrUpdateBatch(scheduling);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "更新排班")
    @PutMapping("/update")
  //  @PreAuthorize("hasAuthority('scheduling:update')")
    public Object update(@Validated(Update.class) @RequestBody Scheduling scheduling) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        Date frontStarTime = dateFormat.parse(scheduling.getFrontStarTime());
        frontStarTime.setYear(2021);
        Date frontEndTime =dateFormat.parse( scheduling.getFrontEndTime());
        frontEndTime.setYear(2021);

        scheduling.setSchedulingStart(frontStarTime);
        scheduling.setSchedulingEnd(frontEndTime);

        Boolean bool= schedulingService.updateById(scheduling);
        if(bool){
             return K.ok().data(scheduling.getId());
        }else{
             return K.error();
        }
    }


    @ApiOperation(value = "删除排班")
    @DeleteMapping("/delete/{id}")
   // @PreAuthorize("hasAuthority('scheduling:delete')")
    public Object delete(@PathVariable("id") Long id){

        Boolean bool= schedulingService.removeById(id);
        if(bool){
            return K.ok();
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "根据排班名称分页获取排班列表")
    @GetMapping("/list")
   // @PreAuthorize("hasAuthority('scheduling:list')")
    public Object getList(Scheduling scheduling,
    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){

        Integer period = scheduling.getPeriod();
        QueryWrapper<Scheduling> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(period)){

            queryWrapper.eq("period",period);

        }
        Page<Scheduling> page = new Page<>(pageNum, pageSize);
        IPage<Scheduling> info=schedulingService.page(page,queryWrapper);

        return K.ok().data(info);
    }

    @ApiOperation(value = "根据编号查询排班信息")
    @GetMapping("/{id}")
    //  @PreAuthorize("hasAuthority('scheduling:get')")
    public Object getItem(@PathVariable("id") Long id){
        Scheduling scheduling=schedulingService.getById(id);
        return K.ok().data(scheduling);
    }
    @ApiOperation(value = "获取酒店下的所有排班规则")
    @GetMapping("getlistall")
    public Object getlistall(){
        UmsMember currentMember = umsAdminService.getCurrentMember();
        if(StringUtils.isEmpty(currentMember)){
            return K.error();
        }
        QueryWrapper<Scheduling> queryWrapper=new QueryWrapper<>();
       queryWrapper.eq("hotel_id",currentMember.getHotelId());
        List<Scheduling> list = schedulingService.list(queryWrapper);
        if(list.size()>0){
            return K.ok().data(list);
        }else{
            return K.error().message("还没有设置排班规则");
        }

    }
}